import axios, { AxiosRequestConfig } from "axios";
//网络封装请求
import router from "../router";
export function request(config: AxiosRequestConfig<any>) {
    const instance = axios.create({
        baseURL: "http://localhost:3001",
        timeout: 50000,
    });
    //请求拦截
    instance.interceptors.request.use(
        (config) => {
            const token = window.localStorage.getItem("token");
            if (token && config && config.headers) {
                config.headers.Authorization = `Bearer ${token}`;
            }
            return config;
        },
        (err) => {
            console.log(err, '请求错误');
        }
    );
    //响应拦截
    instance.interceptors.response.use(
        (res) => {
            return res.data ? res.data : res;
        },
        (error) => {
            //如果有需要授权，才可以访问的接口，同意去login授权
            if (error.response.status == "401") {
                alert("您尚未登录！");
                // router.push({ path: "/index" });
            }
            if (error.response.status == "500") {
                console.log(error.response.data.message);
                // router.push({ path: "/index" });
                return error.response.data.message
            }
            alert('请求超时')
            //如果有错误，在这里面会处理，显示错误信息
        }
    );
    return instance(config);
}
